#include "stdafx.h"

#include "AppHelper.h"
#include "SocketManger.h"
#include "DataPackage.h"
#include "DataParse.h"

int SocketManger::iPort = 0;
SocketClient * SocketManger::pStaticClient = NULL;
QHostAddress SocketManger::addrIP = QHostAddress::LocalHost;

/***********************************************************************************************************************************/
SocketManger::SocketManger(QObject *parent)
	: QObject(parent)
{
	pRecord = NULL;
	//pStaticClient = getStaticClient();
	initSocketClient();

	if (pSocketClient)
	{
		connect(pSocketClient, SIGNAL(disconnected()), this, SLOT(onSocketDisconnected()));
	}
}

SocketManger::~SocketManger()
{
	if (pSocketClient)
	{
		pSocketClient->disconnectFromHost();
		pSocketClient->deleteLater();
		pSocketClient = NULL;
	}

	CHK_POINT_DESTORY(pRecord);
}

/***********************************************************************************************************************************/
SocketClient * SocketManger::getStaticClient()
{
	if (pStaticClient == NULL)
	{
		initData();
		pStaticClient = new SocketClient;
		if (!pStaticClient->connectToServer(addrIP, iPort))
		{
			pStaticClient->deleteLater();
			pStaticClient = NULL;
		}
	}
	return pStaticClient;
}

/***********************************************************************************************************************************/
bool SocketManger::check(QString & Msg)
{
	DataPackage pack;
	QByteArray send;
	bool req = pack.request(send);
	QByteArray received;
	req = pSocketClient->requestData(send, received, Msg);
	qDebug() << req << " : " << received;
	return req;
}

bool SocketManger::exec(const QString & SQL, QString & Msg)
{
	DataPackage pack;
	QByteArray send;
	bool req = pack.request(SQL, JDT_ExecSQL, send);

	if (!req)
	{
		Msg = pack.getError();
		return false;
	}

	if (pSocketClient)
	{
		QByteArray received;
		if (pSocketClient->requestData(send, received, Msg))
		{
			DataParse parse;
			if (parse.parse(received))
			{
				if (parse.getType() == JDT_Result)
				{
					return parse.getResponse(Msg);
				}
			}
		}
	}
	
	Msg = tr("Connection loss or data send receive failed!");
	return false;
}

bool SocketManger::select(const QString & SQL, QRecord & dsRecord, QString & Msg)
{
	DataPackage pack;
	QByteArray send;
	bool req = pack.request(SQL, send);
	
	if (!req)
	{
		Msg = pack.getError();
		return false;
	}

	if (pSocketClient)
	{
		QByteArray received;
		if (pSocketClient->requestData(send, received, Msg))
		{
			DataParse parse;
			if (parse.parse(received))
			{
				if (parse.getType() == JDT_Records)
				{
					if (parse >> dsRecord)
						return true;
				}
				else if (parse.getType() == JDT_Result)
					parse.getResponse(Msg);
				else
					Msg = parse.getErrorMsg();
			}
		}
	}
	else
	{
		Msg = tr("Connection loss or data send receive failed!");
	}

	return false;
}

/***********************************************************************************************************************************/
void SocketManger::onSocketDisconnected()
{
	if (pSocketClient)
	{
		disconnect(pSocketClient, SIGNAL(disconnected()), this, SLOT(onSocketDisconnected()));
		pSocketClient->deleteLater();
		pSocketClient = NULL;
	}
}

/***********************************************************************************************************************************/
void SocketManger::initData()
{
	if (gMapConfigs.count() == 0)
	{
		QSettings * config = getConfig();
		readConfig(*config);
	}

	if (gMapConfigs.contains("SERVER/IP"))
	{
		QString strIP = gMapConfigs.value("SERVER/IP").toString();
		addrIP = QHostAddress(strIP);
	}

	if (gMapConfigs.contains("SERVER/PORT"))
	{
		iPort = gMapConfigs.value("SERVER/PORT").toInt();
	}
}

void SocketManger::initSocketClient()
{
	if (addrIP.isNull() || iPort == 0)
	{
		initData();
	}

	pSocketClient = new SocketClient;
	if (!pSocketClient->connectToServer(addrIP, iPort))
	{
		pSocketClient->deleteLater();
		pSocketClient = NULL;
	}
}


/***********************************************************************************************************************************/
